package com.ruoyi.edh.service.impl;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.edh.domain.model.EdhTeamleaderModel;
import com.ruoyi.edh.mapper.EdhTeamLeaderMapper;
import com.ruoyi.edh.service.IEdhTeamLeaderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @version :
 * @program: easyBuy
 * @description: 易订货-团长管理serviceimpl
 * @author: 周爱鸿
 * @create: 2020-09-03 16:25
 **/
@Service
public class EdhTeamLeaderServiceImpl implements IEdhTeamLeaderService {

	@Autowired
	private EdhTeamLeaderMapper teamLeaderMapper ;
	/**
	 * 获取我的邀请
	 */
	@Override
	public Map<String,Object> getMyTeam() {
		Long userId = SecurityUtils.getUserId();
		List<Map<String, Object>> list = teamLeaderMapper.getMyTeam(userId);
		Map<String,Object> result = new HashMap<String, Object>(16);
		result.put("count",null != list?list.size():0);
		result.put("list",list);
		return result ;
	}

	/**
	 * 获取我的团长邀请成员数和头像
	 */
	@Override
	public Map<String, Object> getMyLeader() {
		SysUser user  = SecurityUtils.getLoginUser().getUser();
		List<String> imageList = new LinkedList<String>();
		List<Map<String, Object>> list = teamLeaderMapper.getMyTeam(user.getInvitees());
		if (null != list) {
			list.forEach(map -> {
				String avatar;
				if (null != map.get("avatar") && !"".equals(avatar = (String) map.get("avatar"))) {
					imageList.add(avatar);
				}
			});
		}
		Map<String,Object> leader = teamLeaderMapper.getMyLeader(user.getUserId());
		Map<String,Object> result = new HashMap<String, Object>(16);
		result.put("count",null != list?list.size():0);
		result.put("imagelist",imageList);
		result.put("leader",leader);
		return result;
	}

	/**
	 * 获取我的邀请
	 * @param type -1全部 1直卖 2组团
	 */
	@Override
	public List<Map<String,Object>> revenueSource(Integer type ) {

		Long userId = SecurityUtils.getUserId();

		List<Map<String,Object>> list = new LinkedList<Map<String, Object>>();
		switch (type){
			case 1 :list =  teamLeaderMapper.getDirectBill(userId);break;
			case 2 :list =  teamLeaderMapper.getGroupBill(userId);break;
			default:list =  teamLeaderMapper.getGeneralLedger(userId);break;
		}
		return list;
	}

	/**
	 * 团员详情
	 *
	 * @param userId 团员id
	 */
	@Override
	public Map<String, Object> memberDetails(Long userId) {

		// 团员消费情况
		Map<String, Object> memberDetail = teamLeaderMapper.consumptionDetail(userId);

		// 团员消费订单记录
		List<Map<String,Object>> list  = teamLeaderMapper.orderRecordList(userId);

		Map<String,Object> result = new HashMap<String, Object>(16);
		result.put("memberDetail",memberDetail);
		result.put("list",list);

		return result;
	}

	/**
	 * 获取团长列表
	 * @param teamleader
	 */
	@Override
	public List<Map<String, Object>> getList(EdhTeamleaderModel.SearchMiodel teamleader) {
		return teamLeaderMapper.getList(teamleader);
	}

	public static void main(String[] args) {
		System.out.println(" SELECT `user`.user_id id, `user`.avatar , `user`.nick_name nickName ,`user`.team_leader  teamLeader,`user`.phonenumber " +
				" ,concat(DATE_FORMAT(`user`.create_time,'%y-%m-%d'),'成为团员') createTime , COUNT(0) consumptionTimes " +
				" ,ifnull(sum(actual_amount),0) totalConsumption , ifnull(SUM(orde.goods_number*specification.commission)) profit" +
				" from edh_order orde " +
				" LEFT JOIN sys_user user on orde.user_id = `user`.user_id " +
				" LEFT JOIN edh_goods_specifications specification on orde.goods_specifications_id = specification.id" +
				" where orde.order_status = 2 and orde.del_flag = 0 and `user`.user_id = #{userId}  ");
	}

	/**
	 * 更新团长标签
	 *
	 * @param teamleader
	 */
	@Override
	public Integer updateLabel(EdhTeamleaderModel.Update teamleader) {
		return teamLeaderMapper.updateLabel(teamleader.getId(),teamleader.getLabel());
	}

	@Override
	public Map<String, Object> teamer(Long userId) {
		return teamLeaderMapper.teamer(userId);
	}
}
